﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BMLib;
namespace BrainMatch
{
    public partial class WaveBall : UserControl
    {
        private WaveManager m_Manager = null;
        private WaveItem m_Item = null;
        private Bitmap m_Bitmap = null;
        private int m_BallWidth = 0;
        private int m_BallHeight = 0;
        private int m_Marge = 10;
        public static List<WaveBall> gBalls = new List<WaveBall>();
        public static WaveBall gSelectBall = null;

        public WaveItem Item
        {
            get
            {
                return m_Item;
            }
            set
            {
                m_Item = value;
                UpdateType();
            }
        }

        public WaveBall(WaveItem item,WaveManager manager)
        {
            m_Manager = manager;
            m_Item = item;
            InitializeComponent();
            MakeBitmap();
            gBalls.Add(this);
            label2.Text = m_Item.WaveName;
            UpdateType();
        }
        private void MakeBitmap()
        {
            m_BallHeight = pictureBox1.Height - m_Marge;
            m_BallWidth = pictureBox1.Width - m_Marge;
            m_Bitmap = new Bitmap(m_BallWidth, m_BallHeight, PixelFormat.Format32bppArgb);
            Pen pen = new Pen(Color.FromArgb(128, 255, 0, 0), 1);

            m_Item.DrawToBitmap(m_Bitmap,pen,true,true);
            pictureBox1.Image = m_Bitmap;
            UpdateType();
        }
        public void UpdateType()
        {
            DataRow[] rows = m_Manager.Dataset.label_names.Select(string.Format("id={0}", m_Item.TypeID));
            if (rows.Length <= 0) return;
            DataRow[] tmprows = m_Manager.Dataset.label_quality.Select(string.Format("id={0}", m_Item.QualityID));
            if (tmprows.Length <= 0) return;

            m_Item.WaveType = rows[0]["name"].ToString();
            m_Item.Quality = tmprows[0]["name"].ToString();
            label1.Text = string.Format("{0}({1})", m_Item.WaveType, m_Item.Quality);
            /*string[] tmp = rows[0]["name"].ToString().Split('_');
            if (tmp.Length > 1)
            {
                m_Item.WaveType = tmp[0];
                m_Item.Quality = tmp[1];

                label1.Text =string.Format("{0}({1})",tmp[0],tmp[1]);
            }
            else
            {
                label1.Text = tmp[0];
            }*/
            // string.Format("{0}({1})", rows[0]["name"].ToString(), m_Item.Quality);
            label1.BackColor = BMUtility.StringToColor(rows[0]["color"].ToString());
        }
        private void OnSelect(object sender, EventArgs e)
        {
            foreach (WaveBall ball in gBalls)
                ball.BackColor = Color.Transparent;
            BackColor = Color.FromArgb(50, 50, 255);
            gSelectBall = this;
        }


        private void OnFixToSelect(object sender, EventArgs e)
        {
            ToolStripMenuItem m = sender as ToolStripMenuItem;
            if (m.Tag == null) return;
            if (m.OwnerItem.Tag == null) return;
            WaveDataSet.label_namesRow nameRow = m.OwnerItem.Tag as WaveDataSet.label_namesRow;
            WaveDataSet.label_qualityRow row = m.Tag as WaveDataSet.label_qualityRow;

            //if (NameBall.gSelectBall == null) return;
            m_Item.TypeID = nameRow.id;// NameBall.gSelectBall.DataRow.id;
            m_Item.QualityID = row.id;

            label1.Text = nameRow.name;// NameBall.gSelectBall.DataRow.name;
            label1.BackColor = BMUtility.StringToColor(nameRow.color); //BMUtility.StringToColor(NameBall.gSelectBall.DataRow.color);
            m_Manager.InsertData(m_Item);

            m_Manager.Windows.ReMatch();
        }

        private void OnSetQulity(object sender, EventArgs e)
        {
            ToolStripMenuItem m = sender as ToolStripMenuItem;
            m_Item.Quality = m.Text;
            UpdateType();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            /*if (gSelectBall != this||NameBall.gSelectBall==null)
            {
                e.Cancel = true;
                return;
            }*/

            OnSelect(this, null);
            contextMenuStrip1.Items.Clear();

            

            foreach (var nameRow in m_Manager.Dataset.label_names)
            {
                ToolStripMenuItem menu = new ToolStripMenuItem();
                contextMenuStrip1.Items.Add(menu);
                //nameMenu.DropDownItems.Clear();
               // string text = string.Format("修正到{0}分类", NameBall.gSelectBall.DataRow.name);
                menu.Text = string.Format("修正到{0}分类", nameRow.name);
                menu.Tag = nameRow;
                foreach (var row in m_Manager.Dataset.label_quality)
                {
                    ToolStripMenuItem m = new ToolStripMenuItem();
                    m.Text = row.name;
                    m.Tag = row;
                    m.Click += new EventHandler(OnFixToSelect);
                    menu.DropDownItems.Add(m);
                }
            }
        }
    }
}
